<?php
/**
 * This is the dispatcher file which routes every request.
 * If it can find a response for the request in the page cache, it outputs that response.
 * Otherwise, it determines which Controller and Action should be used to process the request.
 *
 * @author     Sam Eubank <sam@piephp.com>
 * @package    PiePHP
 * @since      Version 0.0
 * @copyright  Copyright (c) 2010, Pie Software Foundation
 * @license    http://www.piephp.com/license
 */

// Strict error handling promotes less error-prone code.
error_reporting(E_ALL);
set_error_handler('error_handler', E_ALL);

// This is a PiePHP app, so load it up!
require dirname(dirname(dirname(dirname(__FILE__)))) . '/classes/Pie.php';

// $SITE_DIR is the directory which contains this application's files.
Pie::setSitePath(dirname(dirname(__FILE__)) . '/');
// or Pie::setSitePath('piephp.com'); for example

// In development environments, we should use localConfig.php to override this value.
$ENVIRONMENT = 'production'; // 'development', 'test', 'staging', 'production'

// The version number is used when generating minified JavaScript and CSS for a deployment.
$VERSION = '0.0.4';

$COPYRIGHT = 'Copyright &copy; 2007-2011, Pie Software Foundation, All rights reserved.';
$AUTHOR = 'YOUR_NAME <YOUR_EMAIL>';
$LICENSE = 'http://www.piephp.com/license';

$REFRESHER_FILE = 'THE_PATH_TO_A_FILE_THAT_YOUR_EDITOR_TOUCHES_WHEN_IT_SAVES_A_FILE';
$SALT = 'A_SECRET_STRING_FOR_YOUR_SESSION_SECURITY';

/**
 * The configuration settings below are the default configuration for deployments
 * of this application.  Local configurations can override the defaults using
 * their own "localConfig.php", which should be ignored by source control clients.
 */

// Database configurations are included here, but we do not make database connections
// until data is needed.  In many cases, we can try getting data from a cache first.
//   'mysql:host=localhost dbname=piephp user=YOUR_USERNAME password=YOUR_PASSWORD';
//   'pg:host=localhost port=5432 dbname=piephp user=YOUR_USERNAME password=YOUR_PASSWORD';
$DATABASES = array(
	'default' => 'mysql:host=localhost username=piephp password=password database=piephp'
);

// Caches can be used for things like query results and full-page HTML.
$CACHES = array(
	'default' => 'file:host=localhost port=11211 prefix=piephp_ expire=600',
	'pages' => 'file:host=localhost port=11211 prefix=piephp_pages_ expire=600'
);


// $URL_ROOT is the part of the URL that spans from the server name (and port, 
// if any) to the controller name.  This is the URL for the dispatcher, and is
// usually "/" or "/index.php/".
// For deployments to servers without mod_rewrite, this can be changed in localConfig.php.
$URL_ROOT = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
if ($URL_ROOT != '/') 
	$URL_ROOT .= '/';

// Any of the settings above can be overridden in by rewriting them in localConfig.php.
if (file_exists($SITE_DIR . 'localConfig.php'))
	include $SITE_DIR . 'localConfig.php';

// Enable minification based on "release" mode.
switch ($ENVIRONMENT) {
case 'development':
	$MINIFY = false;
	$REFRESHER_ENABLED = true;
	break;
case 'test':
	$MINIFY = true;
	$REFRESHER_ENABLED = false;
	break;
case 'staging':
case 'production':
	$MINIFY = true;
	$REFRESHER_ENABLED = false;
	break;
default:
	throw new Exception("unexpected ENVIRONMENT type: $ENVIRONMENT");
}

// Run the app! ... uh... render the page.
Pie::dispatch();

/**
 * Handle PHP errors using the ErrorsController.
 * @param  $level: the PHP error level.
 * @param  $message: the message describing the error.
 * @param  $file: the file in which the error occurred.
 * @param  $lineNumber: the line number on which the error occurred.
 * @param  $context: the values of local variables at the time when the error happened.
 * @return true to prevent default error handling.
 */
function error_handler($level, $message, $file, $lineNumber, $context) {
	return Pie::onError($level, $message, $file, $lineNumber, $context);
}

